package cn.java88.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.java88.entity.Train;
import cn.java88.util.DBConn;

public class TrainDao {

	private Connection conn;
	
	/**
	 * 查询所有记录
	 * @throws SQLException 
	 */
	public List<Train> selectAll() throws SQLException{
		List<Train> list=new ArrayList<Train>();
		conn=DBConn.getConnection();
		QueryRunner qr=new QueryRunner();
		String sql="select * from train_info";
		list=qr.query(conn, sql, new BeanListHandler<Train>(Train.class));
		DBConn.closeConnection(conn);
		return list;
	}
	/**
	 * 根据id查询记录
	 * @throws SQLException 
	 */
	public Train selectTrainById(String id) throws SQLException {
		Train train=null;
		conn=DBConn.getConnection();
		QueryRunner qr=new QueryRunner();
		String sql="select * from train_info where train_no=?";
		train=qr.query(conn, sql, new BeanHandler<Train>(Train.class),id);
		DBConn.closeConnection(conn);
		return train;
	}
	/**
	 * 根据id删除记录
	 * @throws SQLException 
	 * 
	 */
	public int delete(String t) throws SQLException {
		int i=0;
		conn=DBConn.getConnection();
		QueryRunner qr=new QueryRunner();
		String sql="delete from train_info where train_no=? ";
		i=qr.update(conn, sql,t);
		DBConn.closeConnection(conn);
		return i;
	}
	
}
